package com.wcl.demo.ui.adapter;

import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.android.volley.Response;
import com.wcl.demo.R;
import com.wcl.demo.model.entity.RecommendSoftData;
import com.wcl.demo.network.VolleySingleton;
import com.wcl.demo.ui.activity.iteminterface.OnDownLoadClickListener;

import java.util.List;

/**
 * Created by dllo on 15/12/7.
 */
public class BackgroundDownLoadAdapter extends RecyclerView.Adapter<BackgroundDownLoadAdapter.RecommendBackgroundDownLoadAdapterViweHolder>{

    private List<RecommendSoftData> list;
    private OnDownLoadClickListener listener;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();//定义Volley请求


    public void setOnDownLoadClickListener(OnDownLoadClickListener listener) {
        this.listener = listener;
    }

    public void addData(List<RecommendSoftData> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    class RecommendBackgroundDownLoadAdapterViweHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private ImageView softIv;
        private TextView soft_download_times, softTv;
        private Button button;
        private ProgressBar progressBar;

        public RecommendBackgroundDownLoadAdapterViweHolder(View itemView) {
            super(itemView);
            softIv = (ImageView) itemView.findViewById(R.id.softIv);
            soft_download_times = (TextView) itemView.findViewById(R.id.soft_download_times);
            softTv = (TextView) itemView.findViewById(R.id.softTv);
            button = (Button) itemView.findViewById(R.id.button);
            button.setOnClickListener(this);
            progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar);
            progressBar.setMax(100);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.button:
                    progressBar.setVisibility(View.VISIBLE);
                    if (list != null && list.size() > 0) {
                        if (listener != null) {
                            listener.onDownloadClick(progressBar, list.get(getLayoutPosition()).getDown_url());
                        }
                    }
                    break;
            }
        }
    }

    @Override
    public RecommendBackgroundDownLoadAdapterViweHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View view = inflater.inflate(R.layout.activity_background_download_item, null);

        return new RecommendBackgroundDownLoadAdapterViweHolder(view);
    }

    @Override
    public void onBindViewHolder(RecommendBackgroundDownLoadAdapterViweHolder holder, int position) {
        if (list != null && list.size() > 0) {
            RecommendSoftData data = list.get(position);
            holder.softTv.setText(data.getName());
//            long a = (Long.parseLong(data.getDownload_times()) / 1000000);

            holder.soft_download_times.setText(data.getDownload_times());
            String path = data.getLogo_url();
            getBitmap(path , holder.softIv);
        }
    }

    /***
     * 图片请求
     * @param bitmap
     * @param imageView
     */
    private void getBitmap(String bitmap, final ImageView imageView) {
        volleySingleton.getImageResuest(bitmap, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null && list.size() > 0 ? list.size() : 0;
    }

}
